<?php
class Compete extends BlockInstance {
	public $CI;
	
	function Compete()
	{
		$this->CI =& get_instance();
	}
	
	function __call($m, $a)
	{
		$content = $a[1];
		
		if (strlen($content->data_url))
		{
			$api_key = "k7khdgc2ms7k2ttm3eqppntp";
			
			$url = preg_replace('/^http\:\/\//i', '', $content->data_url);
			$ch  = curl_init();
			curl_setopt($ch, CURLOPT_URL, 'http://api.compete.com/fast-cgi/MI?d='.$url.'&ver=3&apikey='.$api_key.'&size=large');
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type' => 'text/xml; charset=utf-8'));
			$ret = curl_exec($ch);
			curl_close($ch);
			
			// response xml is in $ret
			$ret = '<?xml version="1.0" encoding="utf-8"?>'."\n".$ret;
			$xml = new SimpleXMLElement($ret, LIBXML_NOERROR);
			
			$data['trust'] = $xml->dmn->trust->icon;
			$data['rank']  = $xml->dmn->rank->val;
			$data['metr']  = $xml->dmn->metrics->icon;
			$data['cpurl'] = $xml->dmn->metrics->link;
			$data['inst']  = $a[0]['data'];
			$this->CI->load->view('blocks/compete/content', $data);
		}
		else
		{
			echo 'No URL found for this entry.';
		}
	}
}
?>